package com.leetcode;

import java.util.HashMap;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode
 * @Author: lei
 * @CreateTime: 2024-03-28  13:48
 * @Description: TODO
 * @Version: 1.0
 */
public class 访问完所有房间的第一天 {
    public static void main(String[] args) {
        //int[] nextVisit = {0, 0, 2};
        //int[] nextVisit = {0,1,2,0};
        int[] nextVisit = {0, 0};
        System.out.println(firstDayBeenInAllRooms(nextVisit));
    }

    public static int firstDayBeenInAllRooms(int[] nextVisit) {
        int length = nextVisit.length;
        // key为房间号<0,1,2,3,4>  value为访问该房间的次数
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        int count = 0;
        for (int i = 0; i < length; i++) {
            hashMap.put(i, count);
        }
        // 第一天访问0号房间
        int startIndex = 0, countResult = 0;
        while (startIndex < length) {
            // 获取该房间存储的房间号为 nextVisit[0]
            int first = nextVisit[startIndex];
            // 从hashMap：key为房间号，value为房间的访问次数
            int value = hashMap.get(startIndex) + 1;
            hashMap.put(startIndex, value);
            startIndex = value % 2 == 0 ? (startIndex + 1) % length : first;
            countResult++;
            if (startIndex == (length - 1)) break;
        }
        return countResult;
    }
}
